<script setup lang="ts">
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElImage, ElSwitch } from 'element-plus'
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import {getProductListApi, getDelPersonalProductApi, getUpdProductSateApi } from '@/api/product'
import { ProductData } from '@/api/product/types'
import { TableColumn } from '@/types/table'
import { useValidator } from '@/hooks/web/useValidator'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'
import { ref, reactive } from 'vue'
import { FormSchema } from '@/types/form'
import { Table } from '@/components/Table'
import { useTable } from '@/views/Product/useTable'



const { t } = useI18n()

const { required } = useValidator()

const schema = reactive<FormSchema[]>([
  {
    field: 'name1',
    label: '商品名称',
    component: 'Input'
  },
  {
    field: 'name2',
    label: '商品分类',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: 'java基础-书籍',
          value: '1'
        }
      ]
    }
  },
  {
    field: 'name3',
    label: '商品状态',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '上线',
          value: '1'
        },
        {
          label: '下线',
          value: '2'
        }
      ]
    }
  },
  {
    field: 'name4',
    label: '商品标签',
    component: 'Input'
  }
])

const columns = reactive<TableColumn[]>([
  {
    field: 'product_code',
    label: t('productTable.productCode')
  },
  {
    field: 'main_image',
    label: t('productTable.mainImage')
  },
  {
    field: 'product_name',
    label: t('productTable.productName'),
    search: {
      show: true
    }
  },
  {
    field: 'currentprice_product',
    label: t('productTable.currentPriceProduct')
  },
  {
    field: 'state',
    label: t('productTable.state')
  },
  {
    field: 'categroy_name',
    label: '商品分类'
  },
  {
    field: 'product_label',
    label: '商品标签'
  },
  {
    field: 'product_sate',
    label: '审核状态',
    formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
     if(cellValue == 2){
       return "审核中"
     }else if(cellValue == 3){
       return "审核通过"
     }else if(cellValue == 4){
       return "审核不通过"
     }
    }
  },
  {
    field: 'action',
    width: '305px',
    label: t('tableDemo.action')
  }
])

//获取商品列表数据
const { register, tableObject, methods } = useTable<ProductData>({
  getListApi: getProductListApi,
  delListApi: getDelPersonalProductApi,
  response: {
    list: 'list',
    total: 'total'
  },
  props: {
    columns
  }
})

const { getList, setSearchParams } = methods

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const { push } = useRouter()
//添加
const AddAction = () => {
  push('/product/product-add')
}
const delLoading = ref(false)
//删除
const delData = async (row: ProductData | null, multiple: boolean) => {
  console.log(row)
  tableObject.currentRow = row
  const { delList, getSelections } = methods
  const selections = await getSelections()
  delLoading.value = true
  await delList(
    multiple ? selections.map((v) => v.product_id) : [tableObject.currentRow?.productId as number],
    multiple
  ).finally(() => {
    delLoading.value = false
  })
}
//查看编辑
const action = (row: ProductData, type: number) => {
  push(`/product/product-${type}?product_id=${row.product_id}`)
}

//日志
const actions = () => {
  push(`/product/operationLog`)
}

const loading = ref(false)

//修改上下架
const changeState = async (row: ProductData ) => {
  getUpdProductSateApi( row.state, row.product_id)
}

</script>

<template>
  <ContentWrap :title="t('searchDemo.search')">
    <Search :schema="schema" @search="setSearchParams" @reset="setSearchParams" />
  </ContentWrap>
  <ContentWrap>
    <ElButton type="primary" @click="AddAction">{{ t('productTable.add') }}</ElButton>
  </ContentWrap>
  <ContentWrap :title="t('productTable.personal')">
    <Table
      :border="true"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :data="tableObject.tableList.slice((tableObject.currentPage-1)*tableObject.pageSize,tableObject.currentPage*tableObject.pageSize)"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      @register="register"
    >
      <template #action="{ row }">
        <ElButton type="primary" @click="action(row, 'bianji')">
          {{ t('productTable.bianji') }}
        </ElButton>
        <ElButton type="success" @click="action(row, 'chakan')">
          {{ t('productTable.chakan') }}
        </ElButton>
        <ElButton type="warning" @click="actions(row, 'rizhi')">
          {{ t('productTable.rizhi') }}
        </ElButton>
        <ElButton type="danger" @click="delData(row, true)">
          {{ t('productTable.del') }}
        </ElButton>
      </template>
      <!--上下架-->
      <template #state="{ row }:{ row: ProductData }">
        <ElSwitch
            v-model="row.state"
            @click="changeState(row)"
            :active-value="1"
            :inactive-value="2"
            active-color="#13ce66"
            inactive-color="#ff4949"
        />
      </template>
      <!--商品图片-->
      <template #main_image="{ row }:{ row: ProductData }">
        <ElImage
            style="width: 60px;height: 60px;"
            :src="row.main_image"
            :preview-src-list="[row.main_image]"
            hide-on-click-modal="true"
            preview-teleported="true"
        />
      </template>
    </Table>
  </ContentWrap>
</template>
<style>
   #fenlei{
     width: 200px;
     float: left;
   }
</style>